package com.hz.zbl.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hz.zbl.client.StudentApi;
import com.hz.zbl.mapper.CourseMapper;
import com.hz.zbl.mapper.UserCourseMapper;
import com.hz.zbl.model.dto.StudentDTO;
import com.hz.zbl.model.dto.StudentScheduleDTO;
import com.hz.zbl.model.dto.UserSelectCourseDTO;
import com.hz.zbl.model.entity.CourseEntity;
import com.hz.zbl.model.entity.UserCourseEntity;
import com.hz.zbl.model.enums.ApiResponseCodeEnum;
import com.hz.zbl.model.request.ApiResponse;
import com.hz.zbl.service.StudentCourseService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class StudentCourseServiceImpl implements StudentCourseService {


    @Resource
    StudentApi studentApi;

    @Resource
    UserCourseMapper userCourseMapper;

    @Resource
    CourseMapper courseMapper;

    @Override
    public int selectCourse(int student_user_id, String course_code) {


        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseIdAndStudentId(student_user_id, course_code);
        CourseEntity courseEntity = courseMapper.selectCourseById(course_code);
        if (courseEntity == null){
            return 0;
        }
        if (userCourseEntity != null){
            return 0;
        }
        ApiResponse res = JSON.parseObject(studentApi.studentInfo(student_user_id), ApiResponse.class);
        if (!res.getCode().equals(ApiResponseCodeEnum.SUCCESS.getCode())){
            return 0;
        }
        StudentDTO studentDTO = JSON.parseObject(JSON.toJSONString(res.getData()), StudentDTO.class);

        UserCourseEntity newEntity = new UserCourseEntity();

        newEntity.setCourse_code(course_code);
        newEntity.setCourse_name(courseEntity.getCourse_name());
        newEntity.setTeacher_user_id(courseEntity.getTeacher_user_id());
        newEntity.setTeacher_realname(courseEntity.getTeacher_realname());
        newEntity.setStudent_user_id(student_user_id);
        newEntity.setStudent_realname(studentDTO.getRealname());
        newEntity.setUser_score(0);
        newEntity.setCourse_id(courseEntity.getId());


        newEntity.setCourse_room(courseEntity.getCourse_room());
        newEntity.setCourse_year(courseEntity.getCourse_year());
        newEntity.setCourse_semester(courseEntity.getCourse_semester());

        courseMapper.updateCourseCurrentSectionCount(courseEntity.getCurrent_section_count() + 1, course_code);
        newEntity.setCurrent_section_count(courseEntity.getCurrent_section_count() + 1);

        return userCourseMapper.addUserCourse(newEntity);
    }

    @Override
    public int cancelCourse(int student_user_id, String course_code) {
        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseIdAndStudentId(student_user_id, course_code);
        CourseEntity courseEntity = courseMapper.selectCourseById(course_code);

        if (userCourseEntity == null){
            return 0;
        }

        courseMapper.updateCourseCurrentSectionCount(courseEntity.getCurrent_section_count() - 1, course_code);
        return userCourseMapper.deleteUserCourse(student_user_id, course_code);
    }

    @Override
    public List<CourseEntity> allCourse() {

        return courseMapper.allCourses();
    }


    //查看对应学生所选的课程信息
    @Override
    public List<UserSelectCourseDTO> selectedCourses(Integer student_id) {
        List<UserCourseEntity> userCourseEntities = userCourseMapper.selectedCourses(student_id);
        List<UserSelectCourseDTO> studentCourses = new ArrayList<>();
        userCourseEntities.forEach(userCourseEntity -> {
            UserSelectCourseDTO dto = new UserSelectCourseDTO();
            dto.setCourse_id(userCourseEntity.getCourse_id());
            dto.setCourse_code(userCourseEntity.getCourse_code());

            CourseEntity courseEntity = courseMapper.selectCourseById(userCourseEntity.getCourse_code());

            dto.setCourse_time(courseEntity.getCourse_time());
            dto.setCourse_profile(courseEntity.getCourse_profile());
            dto.setSection_count(courseEntity.getSection_count());
            dto.setTextbook(courseEntity.getTextbook());
            dto.setTeacher_realname(courseEntity.getTeacher_realname());

            //学分，课程名，总分，及格分，得分....
            dto.setCredits_count(courseEntity.getCredits_count());
            dto.setCourse_name(userCourseEntity.getCourse_name());
            dto.setAll_score(courseEntity.getAll_score());
            dto.setPass_score(courseEntity.getPass_score());
            dto.setMy_score(userCourseEntity.getUser_score());



            if (userCourseEntity.getUser_score() < courseEntity.getPass_score()){
                dto.setCourse_is_pass(false);
            }else {
                dto.setCourse_is_pass(true);
            }


            dto.setCurrent_section_count(courseEntity.getCurrent_section_count());

            studentCourses.add(dto);
        });


        return studentCourses;
    }



    @Override
    public UserSelectCourseDTO certainCourseInfo(int student_user_id, String course_code) {
        UserCourseEntity userCourseEntity = userCourseMapper.selectByCourseIdAndStudentId(student_user_id, course_code);
        CourseEntity courseEntity = courseMapper.selectCourseById(course_code);

        UserSelectCourseDTO dto = new UserSelectCourseDTO();

        dto.setCourse_id(courseEntity.getId());
        dto.setCourse_code(course_code);
        dto.setCourse_name(courseEntity.getCourse_name());
        dto.setCourse_profile(courseEntity.getCourse_profile());
        dto.setCourse_time(courseEntity.getCourse_time());
        dto.setSection_count(courseEntity.getSection_count());
        dto.setCredits_count(courseEntity.getCredits_count());
        dto.setAll_score(courseEntity.getAll_score());
        dto.setPass_score(courseEntity.getPass_score());
        dto.setTextbook(courseEntity.getTextbook());
        dto.setTeacher_realname(userCourseEntity.getTeacher_realname());
        dto.setMy_score(userCourseEntity.getUser_score());
        dto.setCourse_is_pass(userCourseEntity.getUser_score() >= courseEntity.getPass_score());

        dto.setCurrent_section_count(userCourseMapper.courseStudentCount(courseEntity.getCourse_name()));


        return dto;
    }




    //查看学生课表
    @Override
    public List<StudentScheduleDTO> studentSchedule(int student_user_id, Integer course_year, Integer course_semester) {
        List<StudentScheduleDTO> studentSchedule = new ArrayList<>();

        List<UserCourseEntity> semesterSchedule = userCourseMapper.studentSemesterSchedule(student_user_id, course_year, course_semester);


        semesterSchedule.forEach(userCourseEntity -> {
            StudentScheduleDTO studentScheduleDTO = new StudentScheduleDTO();
            CourseEntity courseEntity = courseMapper.selectCourseById(userCourseEntity.getCourse_code());


            studentScheduleDTO.setCourse_room(userCourseEntity.getCourse_room());
            studentScheduleDTO.setCourse_name(userCourseEntity.getCourse_name());
            studentScheduleDTO.setTeacher_realname(userCourseEntity.getTeacher_realname());
            studentScheduleDTO.setCourse_time(courseEntity.getCourse_time());

            studentSchedule.add(studentScheduleDTO);
        });
        return studentSchedule;
    }


}
